package cn.com.dhcc.app.pub.core.job.filedata;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.StatefulMethodInvokingJob;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.base.IocBean;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.exch.cmdata.CmDataExchangeLifecycle;
import cn.com.dhcc.turbo.service.exch.filedata.FileDataExchageLifecycle;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;
import cn.com.dhcc.turbo.service.registry.sync.ISynchronizeToGapService;

/**
 * 网闸定时任务，有状态的
 * @author Administrator
 *
 */
public class DownloadFromGapCronJob extends StatefulMethodInvokingJob {
	private Logger logger = Log.getLogger(LOGTYPE.QUARTZJOB);
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
			BaseInfoService baseInfoService=(BaseInfoService) CM.getBeanByName(IocBean.BASE_INFO_SERVICEIMPL_BEAN);
			NodeInfo localNode;
			try {
				logger.info("["+AppConst.installIp+"]"+"定时任务FileDataGapCronJob开始执行....");
				localNode = baseInfoService.getCurrentNodeInfo();
				 if (localNode!=null) {//节点初始化后才开始定时任务
					 FileDataExchageLifecycle service = (FileDataExchageLifecycle) CM.getBeanByName("fileDataExchageLifecycle");
					 if (NodeInfoParam.isInnerNode()  && AppConst.YES.equals(localNode.getHasGap())) {//如果是内网，并且有网闸
						 //解析基础数据
						 ISynchronizeToGapService synchronizeToGapService=(ISynchronizeToGapService) CM.getBeanByName(AppIocBean.SYNCHRONIZE_TO_GAP_BEAN);
						 synchronizeToGapService.analyzeFromGap(NodeInfoParam.GAP_PARAM.IP, NodeInfoParam.GAP_PARAM.Port, NodeInfoParam.GAP_PARAM.Home, NodeInfoParam.GAP_PARAM.UserName, NodeInfoParam.GAP_PARAM.Password);	
						 
						 //解析结构化数据
						  CmDataExchangeLifecycle cmDataService = (CmDataExchangeLifecycle) CM.getBeanByName(AppIocBean.CM_DATA_EXCHANGE_LIFECYCLE_BEAN);
						  cmDataService.innerEntrepotStore();
						
						  //解析非结构化数据
						  service.innerEntrepotStore(); 
					 }
				 }
				 logger.info("["+AppConst.installIp+"]"+"定时任务FileDataGapCronJob执行结束....");
			} catch (ServiceException e) {
				logger.error("["+AppConst.installIp+"]"+"定时任务FileDataGapCronJob执行时出错:"+e);
			}
      
	}
}
